package com.etc.order.provider.mapper;

import com.etc.order.common.domain.ProductCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductCategoryMapper {
    @Select("SELECT * from product_category WHERE category_id = #{categoryId}")
    @ResultMap("productCategoryMap")
    ProductCategory getOne(@Param("categoryId") Integer categoryId);

    @Results(id = "productCategoryMap", value = {
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "category_type", property = "categoryType"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("SELECT * FROM product_category LIMIT ${pageSize * pageIndex}, ${pageSize}")
    List<ProductCategory> findAll(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Select("SELECT * FROM product_category WHERE category_name = #{categoryName} LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("productCategoryMap")
    List<ProductCategory> findByCategoryName(@Param("categoryName") String categoryName, @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM product_category WHERE category_name LIKE CONCAT('%', #{categoryName}, '%')")
    int getTotalCount(@Param("categoryName") String categoryName);

    /** by lw **/
    @Select("SELECT * from product_category GROUP BY categoryType")
    @ResultMap("productCategoryMap")
    List<ProductCategory> getType();

    @Select("SELECT * FROM product_category WHERE category_type = #{categoryType}")
    @ResultMap("productCategoryMap")
    List<ProductCategory> findByCategoryType(@Param("categoryType") int categoryType);

    @Insert("INSERT INTO product_category VALUES(default, #{category.categoryName}, #{category.categoryType}, now(), now())")
    int save(@Param("category") ProductCategory category);

    @Update("UPDATE product_category SET category_name = #{category.categoryName}, category_type = #{category.categoryType}, update_time = now() WHERE category_name = #{category.categoryName}")
    int update(@Param("category") ProductCategory category);

    @Delete("DELETE FROM product_category WHERE category_id = #{categoryId}")
    int delete(@Param("categoryId") int categoryId);
}
